package com.hyt.it.ogt.ks.controller;


import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.core.exception.NestedBusinessException;
import com.hyt.it.ogt.ks.base.BaseController;
import com.hyt.it.ogt.ks.config.KsBizRunTimeException;
import com.hyt.it.ogt.ks.enums.BizStatusCode;
import com.hyt.it.ogt.ks.model.vo.CandidateScoreVO;
import com.hyt.it.ogt.ks.model.vo.login.ScoreLoginVO;
import com.hyt.it.ogt.ks.service.IExamScoreService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author linjd
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/ks/exam-score")
@Api(tags = "4.考生成绩", value = "成绩查询接口")
@ApiSort(4)
@Slf4j
public class ExamScoreController  extends BaseController {
	
	@Resource
	private IExamScoreService iExamScoreService;

	@ApiOperation(value = "4.1 查询考生成绩", notes = "获取考生的成绩，状态码：20000:成功，41020102: 查询失败, 42040101：异常", response = CandidateScoreVO.class)
	@ApiImplicitParams({
		    @ApiImplicitParam(name = "examId", value = "考试项目id", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true),
		    @ApiImplicitParam(name = "candidateId", value = "考生ID", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true)
	})
	@ApiOperationSort(1)
    @GetMapping("queryScore")
    public Object queryScore(@RequestParam String examId, @RequestParam  String candidateId){
        try {
            CandidateScoreVO cs = iExamScoreService.queryScore(examId, candidateId);
            if(cs == null) {
            	return ApiResponse.builder()
                        .code(BizStatusCode.NOT_EXIST_ADMISSION_NUMBER.getCode())
                        .build();
            }
            return ApiResponse.builder()
                    .code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(cs)
                    .build();
        }catch (NestedBusinessException e){
            log.error(StringUtils.join(BizStatusCode.QUERY_SCORE_FAIL.getCode(), ": "), e.getBusinessMessage());
            return ApiResponse.builder()
                    .code(BizStatusCode.QUERY_SCORE_FAIL.getCode())
                    .build();
        } catch (Exception e) {
            log.error(StringUtils.join(BizStatusCode.QUERY_CANDIDATE_SCORE.getCode(), ": "), e);
            return ApiResponse.builder()
                    .code(BizStatusCode.QUERY_CANDIDATE_SCORE.getCode())
                    .build();
        }
    }
	
	@ApiOperation(value = "4.2 查询考生分数详情", notes = "获取考生的分数详情，状态码：20000:成功，42040103: 查询失败, 42040104：异常", response = CandidateScoreVO.class)
	@ApiImplicitParams({
		    @ApiImplicitParam(name = "examId", value = "考试项目id", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true),
		    @ApiImplicitParam(name = "candidateId", value = "考生ID", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true)
	})
	@ApiOperationSort(2)
    @GetMapping("queryScoreDetails")
    public Object queryScoreDetails(@RequestParam String examId, @RequestParam String candidateId){
        try {

            return ApiResponse.builder()
                    .code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(iExamScoreService.queryScoreDetails(examId, candidateId))
                    .build();
        }catch (NestedBusinessException e){
            log.error(StringUtils.join(BizStatusCode.QUERY_SCORE_DETAIL_FAIL.getCode(), ": "), e.getBusinessMessage());
            return ApiResponse.builder()
                    .code(BizStatusCode.QUERY_SCORE_DETAIL_FAIL.getCode())
                    .build();
        } catch (Exception e) {
            log.error(StringUtils.join(BizStatusCode.QUERY_CANDIDATE_DETAIL_SCORE_FAIl.getCode(), ": "), e);
            return ApiResponse.builder()
                    .code(BizStatusCode.QUERY_CANDIDATE_DETAIL_SCORE_FAIl.getCode())
                    .build();
        }
    }
	
	
	@ApiOperation(value = "4.3 查询兴业银行岗位考试成绩", notes = "获取考生的成绩，状态码：20000:成功，18311: 查询失败, 18008：异常", response = CandidateScoreVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "admissionNumber", value = "考生准考号", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true)
	})
	@ApiOperationSort(3)
    @GetMapping("queryXyScore")
    public Object queryXyScore(String admissionNumber){
        try {        	
        	admissionNumber = admissionNumber.trim();        	
    		
            CandidateScoreVO cs = iExamScoreService.queryXyScore(admissionNumber);
            if(cs == null) {
            	return ApiResponse.builder()
                        .code(BizStatusCode.NOT_EXIST_ADMISSION_NUMBER.getCode())
                        .build();
            }
            return ApiResponse.builder()
                    .code(BizStatusCode.KS_SUCCESS_OK.getCode())
                    .data(cs)
                    .build();
        }catch (NestedBusinessException e){
            log.error(StringUtils.join(BizStatusCode.QUERY_SCORE_FAIL.getCode(), ": "), e.getBusinessMessage());
            return ApiResponse.builder()
                    .code(BizStatusCode.QUERY_SCORE_FAIL.getCode())
                    .build();
        } catch (Exception e) {
            log.error(StringUtils.join(BizStatusCode.QUERY_CANDIDATE_SCORE.getCode(), ": "), e);
            return ApiResponse.builder()
                    .code(BizStatusCode.QUERY_CANDIDATE_SCORE.getCode())
                    .build();
        }
    }

    @ApiOperation(value = "4.4 查询成绩登录接口", response = ScoreLoginVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试项目id", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "code", value = "code", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true),
            @ApiImplicitParam(name = "loginType", value = "登录类型：1-身份证号 ，2-手机号 ，3-准考证号", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true)
    })
    @ApiOperationSort(4)
    @GetMapping("scoreLogin")
    public ApiResponse<ScoreLoginVO> scoreLogin(@RequestParam String examId, @RequestParam  String code, @RequestParam String loginType){
        try {
            code = code.trim();
            ScoreLoginVO vo = iExamScoreService.scoreLogin(examId, code, loginType);
            return ApiResponse.<ScoreLoginVO>builder().code(BizStatusCode.KW_SUCCESS_OK.getCode()).data(vo).build();
        }catch (KsBizRunTimeException e){
            log.error( "查询成绩登录接口失败: {}", e.getCode());
            return ApiResponse.<ScoreLoginVO>builder().code(e.getCode()).build();
        } catch (Exception e) {
            log.error("查询成绩登录接口失败： ", e);
            return ApiResponse.<ScoreLoginVO>builder().code(BizStatusCode.QUERY_CANDIDATE_SCORE.getCode()).build();
        }
    }
}
